package com.bjqg.bjqg01.bjqg.activity.address.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.activity.BaseActivity;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by bjqg01 on 2018/8/31.
 */

public class ChangeAddressActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.tv_top_title_drawer)
    TextView tvTopTitleDrawer;
    @BindView(R.id.tv_search_top_drawer)
    TextView tvSearchTopDrawer;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.al_chack_people)
    AutoRelativeLayout alChackPeople;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_xx_address)
    EditText etXxAddress;
    @BindView(R.id.tv_title_home)
    TextView tvTitleHome;
    @BindView(R.id.tv_title_gs)
    TextView tvTitleGs;
    @BindView(R.id.tv_title_school)
    TextView tvTitleSchool;
    @BindView(R.id.tv_add)
    TextView tvAdd;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeaddress);
        ButterKnife.bind(this);
        initdate();
    }

    private void initdate() {
        tvTopTitleDrawer.setText("编辑收货人");
        tvSearchTopDrawer.setText("删除");
    }

    @OnClick({R.id.back, R.id.tv_search_top_drawer, R.id.al_chack_people, R.id.tv_title_home, R.id.tv_title_school, R.id.tv_title_gs})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回
                finish();
                break;
            case R.id.tv_search_top_drawer://删除
                Toast.makeText(this, "删除", Toast.LENGTH_SHORT).show();
                break;
            case R.id.al_chack_people://选择联系人

                startActivityForResult(new Intent(
                        Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), 0);
                break;
            case R.id.tv_title_home:
                initcheck();
                tvTitleHome.setBackgroundResource(R.drawable.bule_rec_bg_30px);
                tvTitleHome.setTextColor(0xffffffff);
                break;
            case R.id.tv_title_school:
                initcheck();
                tvTitleSchool.setBackgroundResource(R.drawable.bule_rec_bg_30px);
                tvTitleSchool.setTextColor(0xffffffff);
                break;
            case R.id.tv_title_gs:
                initcheck();
                tvTitleGs.setBackgroundResource(R.drawable.bule_rec_bg_30px);
                tvTitleGs.setTextColor(0xffffffff);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            ContentResolver reContentResolverol = getContentResolver();
            Uri contactData = data.getData();
            @SuppressWarnings("deprecation")
            Cursor cursor = managedQuery(contactData, null, null, null, null);
            cursor.moveToFirst();
            String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                    null,
                    null);
            while (phone.moveToNext()) {
                String usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                etName.setText(username);
                etPhone.setText(usernumber);
            }

        }
    }


    private void initcheck() {
        tvTitleHome.setBackgroundResource(R.drawable.gray_bg_kuang);
        tvTitleGs.setBackgroundResource(R.drawable.gray_bg_kuang);
        tvTitleSchool.setBackgroundResource(R.drawable.gray_bg_kuang);
        tvTitleGs.setTextColor(0xff000000);
        tvTitleHome.setTextColor(0xff000000);
        tvTitleSchool.setTextColor(0xff000000);
    }
}
